import {simpleGet, simplePost} from "../../../utils/request";
import {showToast, showToastSuccess} from "../../../utils/tool";

const app = getApp()
const global = app.globalData

Page({
    data: {
        nav:{
            opacity:0,
            title:"签到中心",
        },
        imageUrl:global.imageUrl,
        signs:[],
        signDay: 0, // 已连续签到天数
        isTodaySign:0,
    },
    onLoad: function (options={}) {
        this.init()
    },

    // 签到
    signHandle(e){
        const {isTodaySign, signDay} = this.data
        const {index} = e.currentTarget.dataset
        if (isTodaySign){ // 今日已签到
            showToast("今日已签到")
            return;
        }
        if (index == signDay) { // 点击对应日期，才能签到
            simpleGet("/userSign/doSign").then(r => {
                this.setData({
                    signDay: signDay+1,
                })
                showToastSuccess("签到成功")
            })
        }
    },
    init(){
        simpleGet("/userSign/index").then(r => {
            this.setData({
                signs:r.signs,
                signDay:r.signDay,
                isTodaySign:r.isTodaySign,
                totalSignDay:r.totalSignDay,
            })
        })
    }
});
